package com.ruoyi.web.controller.app.pay.alipay.controller;

import com.alipay.api.AlipayApiException;
import com.ruoyi.web.controller.app.pay.alipay.service.IAliPayService;
import com.ruoyi.web.controller.app.pay.util.ResultMap;
import com.ruoyi.web.controller.app.pay.util.StaticCode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;
import java.util.Map;

/**
 * 支付宝支付
 */
@Controller
@RequestMapping(value = "alipay")
@Api(tags = {"支付宝支付"})
public class AliPayController {
    private static final Logger logger = LoggerFactory.getLogger(AliPayController.class);

    @Autowired
    private IAliPayService aliPayService;

    @RequestMapping(value = "appPay", method = RequestMethod.POST)
    @ResponseBody
	@ApiOperation(value = "支付宝支付", notes = "支付宝支付")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "userId", value = "用户id", required = true, dataType = "String", paramType = "query"),
		@ApiImplicitParam(name = "orderId", value = "订单id", required = true, dataType = "Long", paramType = "query")

	})
    public Map<String,Object> appPay(String userId, Long orderId) {
        logger.info("app支付服务端");
        try {
            return ResultMap.convertMap(StaticCode.SUCCESS_CODE,aliPayService.appPay(userId, orderId),"");
        } catch (IllegalArgumentException e) {
            return ResultMap.getFailMap("失败，" + e.getMessage());
        } catch (Exception e) {
            return ResultMap.getFailMap("系统错误," + e.getMessage());
        }

    }


    @PostMapping("/notifyUrl")
    public void notifyUrl(HttpServletRequest request, HttpServletResponse response) throws AlipayApiException, UnsupportedEncodingException, org.codehaus.jettison.json.JSONException {
        aliPayService.notifyUrl(request,response);
    }


}
